<template>
  <div class="person">
    <h3>人员列表，目前有{{newsList.length}}条新闻</h3>
    <input type="text" placeholder="姓名" v-model="name">
    <input type="text" placeholder="年龄" v-model.number="age">
    <button @click="handleAdd">添加</button>
    <ul>
      <li v-for="p in personList" :key="p.id">
        {{p.name}} --  {{p.age}}
      </li>
    </ul>
  </div>
</template>

<script>
import { nanoid } from 'nanoid'
import { mapState } from 'vuex'
  export default {
    name:'Person',
    data() {
      return {
        name:'',
        age:''
      }
    },
    computed:{
      ...mapState(['newsList','personList']),
    },
    methods: {
      handleAdd(){
        let obj = {id:nanoid(),name:this.name,age:this.age}
        this.$store.commit('ADD_PERSON',obj)
      }
    },
  }
</script>

<style scoped>
  .person {
    background-color: rgb(220, 144, 5);
    padding: 10px;
  }
</style>